package com.cn.crmsystem.service.customerService.impl;

import com.cn.crmsystem.mapper.customerMapper.CustomerMapper;
import com.cn.crmsystem.pojo.*;
import com.cn.crmsystem.service.customerService.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public ResultJson insertCustomer(Customer customer, Login login) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("customer",customer);
        map.put("login",login);
        if(customerMapper.insertCustomer(map)>0){
            return new ResultJson<>().getSuccess("添加成功");
        }else{
            return new ResultJson<>().getSuccess("添加失败");
        }
    }

    @Override
    public ResultJson deleteCustomer(Integer id) {
        if(customerMapper.deleteCustomer(id)>0){
            return new ResultJson<>().getSuccess("删除成功");
        }else{
            return new ResultJson<>().getSuccess("删除失败");
        }
    }

    @Override
    public ResultJson addCustomerFollow(Login login, Follow_Up follow_up) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("login",login);
        map.put("follow",follow_up);
        if(customerMapper.addCustomerFollow(map)>0){
            return new ResultJson<>().getSuccess("添加成功");
        }else{
            return new ResultJson<>().getSuccess("添加失败");
        }
    }

    @Override
    public ResultJson updateCustomer(Customer customer) {
        if(customerMapper.updateCustomer(customer)>0){
            return new ResultJson<>().getSuccess("修改成功");
        }else{
            return new ResultJson<>().getSuccess("修改失败");
        }
    }

    @Override
    public ResultJson updateCustomerUser(User user) {
        if(customerMapper.updateCustomerUser(user)>0){
            return new ResultJson<>().getSuccess("修改成功");
        }else{
            return new ResultJson<>().getSuccess("修改失败");
        }
    }

    @Override
    public ResultJson<List> selectCustomer(Map<String, Object> map) {
        List list =customerMapper.selectCustomer(map);
        Long count=customerMapper.selectCustomerCount(map);
        if(list!=null && list.size()>0){
            return new ResultJson<>().getSuccess("查询成功",list,count);
        }else{
            return new ResultJson<>().getSuccess("查询失败");
        }
    }


    @Override
    public ResultJson deleteAllCustomer(Integer[] ids) {
        int result=0;
        for(int i=0;i<ids.length;i++){
            result=customerMapper.deleteCustomer(ids[i]);
        }
        if(result>0){
            return new ResultJson<>().getSuccess("删除成功");
        }else{
            return new ResultJson<>().getSuccess("删除失败");
        }
    }
}
